home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Processes / MP Threaded Sort / Sprocket / Lib / Window.cp < prev   
Encoding:
Text File  |  1997-03-13  |  23.9 KB  |  979 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.                 
  11.     Written by: Dave Falkenburg
  12.  
  13.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  14.  
  15.     Change History (most recent first):
  16.     
  17.          <8>    11/17/94    DRF        Add casts for CFront & PPCC. Also dealt with the change to
  18.                                     ClipAbove in the latest universal headers.
  19.          <7>    11/12/94    DRF        Added AdjustMenusBeforeMenuSelection.
  20.          <6>     11/8/94    DRF        Add some better menu handling methods.
  21.          <5>     9/27/94    DRF         AppLib.h is now Sprocket.h
  22.          <4>      9/9/94    DRF        Reorganized headers and removed redundant #includes.
  23.          <3>      9/4/94    DRF        Added DrawJustTheGrowIcon.
  24.          <2>     8/27/94    DRF        In TWindow::Close, call window’s (de)Activate method before
  25.                                     closing so that menus can be properly updated.
  26.     
  27.     To Do:        Make sure invisible windows can be created & managed
  28.                 Handle modal windows as another class of windows
  29.                 Fix activate bugs when showing and hiding windows
  30.                 Window positioning methods (getters and setters)
  31.                 Display Manager support
  32.                 Changes to support AEObject model
  33.  */
  34.  
  35. #include "Sprocket.h"
  36. #include "Window.h"
  37.  
  38. #include <Script.h>        //    for GetMBarHeight()
  39. #include <LowMem.h>        //    for LMGetWindowList()
  40.  
  41.  
  42. const short            kFloatingWindowKind        = 1000;
  43. const short            kNormalWindowKind        = 1001;
  44. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  45.  
  46. const short            kScreenEdgeSlop            = 4;
  47. const short            kSpaceForFinderIcons    = 64;
  48. const short            kMinimumTitleBarHeight    = 21;
  49. const short            kMinimumWindowSize        = 32;
  50.  
  51. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  52.  
  53. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  54. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  55.  
  56.  
  57. struct    CalcWindowAreaDeviceLoopUserData
  58.     {
  59.     GDHandle    fScreenWithLargestPartOfWindow;
  60.     long        fLargestArea;
  61.     Rect        fWindowBounds;
  62.     };
  63.  
  64.  
  65.  
  66.  
  67.  
  68. TWindow::TWindow()
  69.     {
  70.     }
  71.  
  72.  
  73. TWindow::~TWindow()
  74.     {
  75.     }
  76.  
  77.  
  78. void
  79. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  80.     {
  81.     WindowPtr    behindWindow,oldFrontMostWindow;
  82.     
  83.     if (typeOfWindowToCreate == kModalWindow)
  84.         {
  85.         DebugStr((StringPtr) "\pModal windows aren’t supported yet");
  86.         fWindowType = kFloatingWindow;
  87.         return;
  88.         }
  89.     else if (typeOfWindowToCreate == kFloatingWindow)
  90.         {
  91.         behindWindow = (WindowPtr) -1;
  92.         oldFrontMostWindow = FrontWindow();
  93.  
  94.         fWindowType = kFloatingWindow;
  95.         }
  96.     else if (typeOfWindowToCreate == kNormalWindow)
  97.         {
  98.         behindWindow = LastFloatingWindow();
  99.  
  100.         fWindowType = kNormalWindow;
  101.         
  102.         if (behindWindow == kNoFloatingWindows)
  103.             oldFrontMostWindow = nil;
  104.         else
  105.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  106.         }
  107.  
  108.     fWindow = this->MakeNewWindow(behindWindow);
  109.     fIsVisible = ((WindowPeek) fWindow)->visible;
  110.  
  111.     if (fWindow)
  112.         {
  113.         SetWRefCon(fWindow,(long) this);
  114.  
  115.         if (typeOfWindowToCreate == kModalWindow)
  116.             {
  117.             DebugStr((StringPtr) "\pCan’t create Modal windows yet");
  118.             }
  119.         else if (typeOfWindowToCreate == kFloatingWindow)
  120.             {
  121.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  122.             
  123.             //    make sure the other window stays hilited
  124.             if (oldFrontMostWindow)
  125.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  126.             }
  127.         else if (typeOfWindowToCreate == kNormalWindow)
  128.             {
  129.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  130.  
  131.             //    unhighlight the old front window
  132.             if (oldFrontMostWindow)
  133.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  134.  
  135.             //    hilite the new window…
  136.             HiliteAndActivateWindow(fWindow,true);
  137.             }
  138.         }
  139.     }
  140.  
  141.  
  142. void
  143. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  144.     {
  145.     }
  146.  
  147. void
  148. TWindow::Idle(EventRecord * /* anEvent */)
  149.     {
  150.     }
  151.     
  152. void
  153. TWindow::Activate(Boolean /* activating */)
  154.     {
  155.     }
  156.     
  157. void
  158. TWindow::Draw(void)
  159.     {
  160.     }
  161.     
  162. void
  163. TWindow::Click(EventRecord * /* anEvent */)
  164.     {
  165.     }
  166.     
  167. void
  168. TWindow::KeyDown(EventRecord * /* anEvent */)
  169.     {
  170.     }
  171.  
  172.  
  173. void
  174. TWindow::Select(void)
  175.     {
  176.     WindowPtr    currentFrontWindow;
  177.     
  178.     if (fWindowType == kFloatingWindow)
  179.         currentFrontWindow = FrontWindow();
  180.     else if (fWindowType == kNormalWindow)
  181.         currentFrontWindow = FrontNonFloatingWindow();
  182.     else
  183.         {
  184.         }
  185.  
  186.     if (currentFrontWindow != fWindow)
  187.         {
  188.         if (fWindowType == kFloatingWindow)
  189.             BringToFront(fWindow);
  190.         else
  191.             {
  192.             WindowPtr    lastFloater = LastFloatingWindow();
  193.  
  194.             //    If there are no floating windows,
  195.             //    just call SelectWindow like the good ol’ days
  196.  
  197.             if (lastFloater == kNoFloatingWindows)
  198.                 SelectWindow(fWindow);
  199.             else
  200.                 {
  201.                 // Deactivate the window currently in front.
  202.  
  203.                 HiliteAndActivateWindow(currentFrontWindow,false);
  204.     
  205.                 // Bring it behind the last floating window and activate it.
  206.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  207.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  208.                 // this is no longer necessary.
  209.  
  210.                 SendBehind(fWindow,lastFloater);
  211.                 HiliteAndActivateWindow(fWindow,true);
  212.                 }
  213.             }
  214.         }
  215.     }
  216.  
  217.  
  218. void
  219. TWindow::Drag(Point startPoint)
  220.     {
  221.     GrafPtr        savePort;
  222.     KeyMap        theKeyMap;
  223.     Boolean        commandKeyDown = false;
  224.     RgnHandle    draggingRegion;
  225.     long        dragResult;
  226.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  227.     Boolean     gLiveDrag;
  228.     
  229.     
  230.     if (WaitMouseUp())        //    de-bounce?
  231.         {
  232.         // Set up the Window Manager port.
  233.     
  234.         GetPort(&savePort);
  235.         SetPort(gWindowManagerPort);
  236.         SetClip(GetGrayRgn());
  237.  
  238.         // Check to see if the command key is down.
  239.     
  240.         GetKeys(theKeyMap);
  241.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  242.         gLiveDrag = (theKeyMap[1] >> 2) & 0x01 ;
  243.         
  244.         if (commandKeyDown)
  245.             {
  246.             //    We’re not going to change window ordering,
  247.             //    so make sure that we don’t drag in front of
  248.             //    other windows which may be in front of ours.
  249.  
  250. //    11/16/94    <Windows.h> on ETO defines this routine to take
  251. //                a WindowPtr instead of a WindowPeek. When building
  252. //                with new headers (like those included with ETO 16)
  253. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  254.  
  255. #if    USEOLDUNIVERSALHEADERS
  256.             ClipAbove(windowAsWindowPeek);
  257. #else
  258.             ClipAbove((WindowPtr) windowAsWindowPeek);
  259. #endif
  260.             }
  261.         else if (fWindowType != kFloatingWindow)
  262.             {
  263.             //    We’re dragging a normal window, so make sure
  264.             //    that we don’t drag in front of any floating
  265.             //    windows.
  266.  
  267. //    11/16/94    <Windows.h> on ETO defines this routine to take
  268. //                a WindowPtr instead of a WindowPeek. When building
  269. //                with new headers (like those included with ETO 16)
  270. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  271.  
  272. #if    USEOLDUNIVERSALHEADERS
  273.             ClipAbove((WindowPeek) FrontNonFloatingWindow());
  274. #else
  275.             ClipAbove(FrontNonFloatingWindow());
  276. #endif
  277.             }
  278.         
  279.         //    Drag an outline of the window around the desktop.
  280.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  281.  
  282.         //if (gLiveDrag) {
  283.             /*Rect r = {0,0,0,0};
  284.             Point origin = {0,0};    
  285.             Point diff;
  286.             EventRecord event;
  287.             GrafPtr tempPort;
  288.             
  289.             
  290.             GetPort(&tempPort);
  291.             SetPort(fWindow);    
  292.             (void)OSEventAvail(everyEvent,&event);
  293.             
  294.             LocalToGlobal(&origin);
  295.             diff.h = ( origin.h - event.where.h);
  296.             diff.v = ( origin.v -event.where.v);
  297.             //origin.h = windowAsWindowPeek->strucRgn[0]->rgnBBox.left;
  298.             //origin.v = windowAsWindowPeek->strucRgn[0]->rgnBBox.top;
  299.             SetPort(tempPort);
  300.             while (StillDown()) {
  301.                 EventRecord event;
  302.                 //short slop;
  303.                 
  304.                 //(void)OSEventAvail(everyEvent,&event);
  305.                 
  306.                 //ValidRect(&goodRect_Rect);
  307.                 (void)WaitNextEvent(everyEvent,&event,1,nil);
  308.                 
  309.                 if (event.what == updateEvt) {
  310.                     WindowRef window = (WindowRef)event.message;
  311.                     
  312.                     
  313.                     if (window) {
  314.                         TWindow * wobj;
  315.                         
  316.                         SetPort(window);
  317.                         BeginUpdate(window);
  318.                         wobj = GetWindowObject(window);
  319.  
  320.                         wobj->Draw();
  321.                         EndUpdate(window);
  322.                         SetPort(tempPort);
  323.                     }
  324.                     
  325.                     
  326.                 }
  327.                 
  328.                 event.where.h += diff.h ;
  329.                 event.where.v += diff.v;
  330.                 
  331.  
  332.                 
  333.                 
  334.                 if (coplandTask)
  335.                 MoveWindow(fWindow,event.where.h,event.where.v,true);
  336.  
  337.                 Idle(nil);
  338.                 
  339.                 dragResult = 0;
  340.                 
  341.             }
  342.             */
  343.         //} else {
  344.             draggingRegion = NewRgn();
  345.             CopyRgn(windowAsWindowPeek->strucRgn,draggingRegion);
  346.             dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  347.             DisposeRgn(draggingRegion);
  348.  
  349.         //}
  350.         SetPort(savePort);    //    Get back to old port
  351.  
  352.         if ((dragResult != 0) && (dragResult != 0x80008000))
  353.             {
  354.             this->Nudge((short) (dragResult & 0xFFFF),(short) (dragResult >> 16));
  355.             }
  356.         }
  357.  
  358.     if (!commandKeyDown)
  359.         Select();
  360.     }
  361.  
  362. void
  363. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  364.     {
  365.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  366.     short        newHorizontalPosition,newVerticalPosition;
  367.     
  368.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  369.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  370.  
  371.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  372.     }
  373.  
  374. void
  375. TWindow::Grow(Point startPoint)
  376.     {
  377.     GrafPtr    oldPort;
  378.     long    newSize;
  379.     Rect    oldWindowRect,resizeLimits;
  380.     
  381.     GetPort(&oldPort);
  382.     
  383.     GetWindowSizeLimits(&resizeLimits);
  384.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  385.     if (newSize)
  386.         {
  387.         oldWindowRect = fWindow->portRect;
  388.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  389.         SetPort(fWindow);
  390.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  391.         }
  392.     
  393.     SetPort(oldPort);
  394.     }
  395.  
  396.  
  397. void
  398. TWindow::Zoom(short zoomState)
  399.     {
  400.     GrafPtr        oldPort;
  401.     FontInfo    systemFontInfo;
  402.     short        titleBarHeight;
  403.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  404.     short        amountOffscreen;
  405.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  406.     GDHandle    bestDevice;
  407.     
  408.     GetPort(&oldPort);
  409.  
  410.     //    Figure out the height of the title bar so we can properly position
  411.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  412.     //
  413.     //    This probably isn’t the best thing to do: A better way might be 
  414.     //    to diff the structure and content region rectangles?
  415.  
  416.     SetPort(gWindowManagerPort);
  417.     GetFontInfo(&systemFontInfo);
  418.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  419.     if ((titleBarHeight % 2) == 1)
  420.         titleBarHeight--;
  421.     if (titleBarHeight < kMinimumTitleBarHeight)
  422.         titleBarHeight = kMinimumTitleBarHeight;
  423.  
  424.  
  425.     //    Only do the voodoo magic if we are really “zooming” the window.
  426.  
  427.     if (zoomState == inZoomOut)
  428.         {
  429.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  430.         bestScreenRect.top += titleBarHeight;
  431.  
  432.         this->GetPerfectWindowSize(&perfectWindowRect);
  433.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  434.  
  435.         //    Take the zero-pined perfect window size and move it to
  436.         //    the top left of the    window’s content region.
  437.  
  438.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  439.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  440.  
  441.         
  442.         //    Does perfectWindowRect fit completely on the best screen?
  443.         
  444.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  445.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  446.             {
  447.             //    SectRect sez perfectWindowRect doesn’t completely fit
  448.             //    on the screen, so bump the window so that more of it fits.
  449.  
  450.             //    Make sure that the left edge of perfectWindowRect is forced
  451.             //    onto the best screen.  This is in case we are bumping
  452.             //    the window to the right.
  453.  
  454.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  455.             if (amountOffscreen > 0)
  456.                 {
  457.                 perfectWindowRect.left += amountOffscreen;
  458.                 perfectWindowRect.right += amountOffscreen;
  459.                 }
  460.  
  461.             //    Make sure that the left edge of perfectWindowRect is forced
  462.             //    onto the best screen.  This is in case we are bumping
  463.             //    the window downward to a new screen.
  464.     
  465.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  466.             if (amountOffscreen > 0)
  467.                 {
  468.                 perfectWindowRect.top += amountOffscreen;
  469.                 perfectWindowRect.bottom += amountOffscreen;
  470.                 }
  471.  
  472.             //    If right edge of window falls off the screen,
  473.             //        Move window to the left until the right edge IS on the screen
  474.             //        OR the left edge is at bestScreenRect.left
  475.  
  476.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  477.             if (amountOffscreen > 0)
  478.                 {
  479.                 //    Are we going to push the left edge offscreen? If so, change the
  480.                 //    offset so we move the window all the way over to the left.
  481.                 
  482.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  483.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  484.  
  485.                 perfectWindowRect.left -= amountOffscreen;
  486.                 perfectWindowRect.right -= amountOffscreen;
  487.                 }
  488.  
  489.             //    If bottom edge of window falls off the screen,
  490.             //        Move window to up until the bottom edge IS on the screen
  491.             //        OR the top edge is at bestScreenRect.top
  492.  
  493.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  494.             if (amountOffscreen > 0)
  495.                 {
  496.                 //    Are we going to push the top edge offscreen? If so, change the
  497.                 //    offset so we move the window just to the top.
  498.                 
  499.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  500.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  501.  
  502.                 perfectWindowRect.top -= amountOffscreen;
  503.                 perfectWindowRect.bottom -= amountOffscreen;
  504.                 }
  505.  
  506.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  507.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  508.                 {
  509.                 //    The edges of the window still fall offscreen,
  510.                 //    so make the window smaller until it fits.
  511.                 
  512.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  513.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  514.  
  515.                 //    If the right edge is still falling off,
  516.                 //        save space for Finder’s disk icons as well.
  517.  
  518.                 if (perfectWindowRect.right > bestScreenRect.right)
  519.                     {
  520.                     perfectWindowRect.right = bestScreenRect.right;
  521.                     
  522.                     //    If we were on the main screen, leave room for Finder icons, too.
  523.                     
  524.                     if (bestDevice == GetMainDevice())
  525.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  526.                     }
  527.                 }
  528.             }
  529.  
  530.         //    Stash our new rectangle inside of the Window’s dataHandle
  531.         //    so that ZoomWindow does the right thing.
  532.         
  533.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  534.         }
  535.  
  536.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  537.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  538.     
  539.     SetPort(fWindow);
  540.  
  541.     Rect    oldWindowRect = fWindow->portRect;
  542.     
  543.     ZoomWindow(fWindow,zoomState,false);
  544.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  545.  
  546.     SetPort(oldPort);
  547.     }
  548.  
  549. void
  550. TWindow::ShowHide(Boolean showFlag)
  551.     {
  552.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  553.     //    the global function, and not the method ShowHide. Unintended recursion
  554.     //    can do bad things to the unsuspecting programmer.
  555.     
  556.     //    Some C++ programmers would always prepend the “::” on function calls.
  557.     
  558.     ::ShowHide(fWindow,showFlag);
  559.     fIsVisible = showFlag;
  560.     }
  561.     
  562.  
  563. Boolean
  564. TWindow::EventFilter(EventRecord * /* theEvent */)
  565.     {
  566.     return false;
  567.     }
  568.     
  569.  
  570. void
  571. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  572.     {
  573.     *perfectSize = qd.screenBits.bounds;
  574.     }
  575.  
  576. void
  577. TWindow::GetWindowSizeLimits(Rect *limits)
  578.     {
  579.     limits->top = limits->left = kMinimumWindowSize;
  580.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  581.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  582.     }
  583.  
  584. void
  585. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  586.     {
  587.     }
  588.  
  589.  
  590. Boolean
  591. TWindow::IsVisible(void)
  592.     {
  593.     return fIsVisible;
  594.     }
  595.  
  596.  
  597. Boolean
  598. TWindow::CanClose(void)
  599.     {
  600.     return true;
  601.     }
  602.  
  603.  
  604. Boolean
  605. TWindow::Close(void)
  606.     {
  607.     WindowPtr    newFrontWindow = nil;
  608.     
  609.     if (FrontNonFloatingWindow() == fWindow)
  610.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  611.  
  612.     this->Activate(false);
  613.     DisposeWindow(fWindow);
  614.  
  615.     if (newFrontWindow)
  616.         HiliteAndActivateWindow(newFrontWindow,true);
  617.  
  618.     return true;
  619.     }
  620.  
  621.  
  622. Boolean
  623. TWindow::DeleteAfterClose(void)
  624.     {
  625.     return true;
  626.     }
  627.  
  628.  
  629. void
  630. TWindow::AdjustMenusBeforeMenuSelection(void)
  631.     {
  632.     }
  633.  
  634.     
  635. void
  636. TWindow::DoMenuSelection(short /* menu */, short /* item */)
  637.     {
  638.     }
  639.     
  640.  
  641. void
  642. TWindow::DoMenuCommand(unsigned long /* menuCommand */)
  643.     {
  644.     }
  645.  
  646.  
  647. OSErr
  648. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  649.     {
  650.     OSErr    result = dragNotAcceptedErr;
  651.     
  652.     switch (dragMessage)
  653.         {
  654.         case    dragTrackingEnterWindow:
  655.             result = this->DragEnterWindow(theDrag);
  656.             break;
  657.         
  658.         case    dragTrackingInWindow:
  659.             result = this->DragInWindow(theDrag);
  660.             break;
  661.             
  662.         case    dragTrackingLeaveWindow:
  663.             result = this->DragLeaveWindow(theDrag);
  664.             break;
  665.             
  666.         default:
  667.             break;
  668.         }
  669.  
  670.     return result;
  671.     }
  672.  
  673.  
  674. OSErr
  675. TWindow::DragEnterWindow(DragReference /* theDrag */)
  676.     {
  677.     return dragNotAcceptedErr;
  678.     }
  679.  
  680.  
  681. OSErr
  682. TWindow::DragInWindow(DragReference /* theDrag */)
  683.     {
  684.     return dragNotAcceptedErr;
  685.     }
  686.  
  687.  
  688. OSErr
  689. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  690.     {
  691.     return dragNotAcceptedErr;
  692.     }
  693.     
  694.  
  695. OSErr
  696. TWindow::HandleDrop(DragReference /* theDrag */)
  697.     {
  698.     return dragNotAcceptedErr;
  699.     }
  700.  
  701.  
  702. ///////////////////////////////////////////////////////////////////////////
  703. //
  704. //    Utility Functions used for floating windows
  705. //
  706.  
  707. TWindow *
  708. GetWindowObject(WindowPtr aWindow)
  709.     {
  710.     short    wKind;
  711.     
  712.     if (aWindow != nil)
  713.         {
  714.         wKind = ((WindowPeek) aWindow)->windowKind;
  715.  
  716.         if (wKind >= userKind)
  717.             {
  718.             //    All windowKinds >= userKind are based upon TWindow
  719.  
  720.             return (TWindow *) GetWRefCon(aWindow);
  721.             }
  722.         }
  723.     return (TWindow *) nil;
  724.     }
  725.  
  726.  
  727. ////////////////////////////////////////////////////////////////////////
  728. //
  729. //    Utility functions
  730.  
  731.  
  732. pascal WindowPtr
  733. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  734.     {
  735.     if (gHasColorQuickdraw)
  736.         return GetNewCWindow(windowID,wStorage,behind);
  737.     else
  738.         return GetNewWindow(windowID,wStorage,behind);
  739.     }
  740.  
  741.  
  742. pascal WindowPtr
  743. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  744.     {
  745.     if (gHasColorQuickdraw)
  746.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  747.     else
  748.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  749.     }
  750.  
  751.  
  752. void
  753. DrawJustTheGrowIcon(WindowPtr aWindow)
  754.     {
  755.     GrafPtr        savedPort;
  756.     RgnHandle    savedClip = NewRgn();
  757.     Rect        growBoxRect;
  758.     
  759.     GetPort(&savedPort);
  760.     SetPort(aWindow);
  761.     GetClip(savedClip);
  762.  
  763.     //    clip to just the bottom right corner of the window
  764.     
  765.     growBoxRect.top = aWindow->portRect.bottom - kScrollbarWidth;
  766.     growBoxRect.bottom = aWindow->portRect.bottom;
  767.     growBoxRect.left = aWindow->portRect.right - kScrollbarWidth;
  768.     growBoxRect.right = aWindow->portRect.right;
  769.     ClipRect(&growBoxRect);
  770.  
  771.     DrawGrowIcon(aWindow);
  772.  
  773.     SetClip(savedClip);
  774.     DisposeRgn(savedClip);    
  775.  
  776.     SetPort(savedPort);
  777.     }
  778.     
  779.  
  780. WindowPtr
  781. LastFloatingWindow(void)
  782.     {
  783.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  784.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  785.     
  786.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  787.         {
  788.         if (aWindow->visible)
  789.             lastFloater = (WindowPtr) aWindow;
  790.  
  791.         aWindow = (WindowPeek) aWindow->nextWindow;
  792.         }
  793.     return(lastFloater);
  794.     }
  795.  
  796.  
  797. WindowPtr
  798. FrontNonFloatingWindow(void)
  799.     {
  800.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  801.  
  802.     //    Skip over floating windows
  803.         
  804.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  805.         aWindow = (WindowPeek) aWindow->nextWindow;
  806.  
  807.     //    Skip over invisible, but otherwise normal windows
  808.     
  809.     while (aWindow && (aWindow->visible == 0))
  810.         aWindow = (WindowPeek) aWindow->nextWindow;
  811.         
  812.     return (WindowPtr) aWindow;
  813.     }
  814.  
  815.  
  816. void
  817. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  818.     {
  819.     GrafPtr        oldPort;
  820.     TWindow    *    wobj = GetWindowObject(aWindow);
  821.     
  822.     if (aWindow)
  823.         {
  824.         HiliteWindow(aWindow,active);
  825.  
  826.         if (wobj != nil)
  827.             {
  828.             GetPort(&oldPort);
  829.             SetPort(aWindow);
  830.             wobj->Activate(active);
  831.             SetPort(oldPort);
  832.             }    
  833.         }
  834.     }
  835.  
  836. void
  837. SuspendResumeWindows(Boolean resuming)
  838.     {
  839.     //    When we suspend/resume, hide/show all the visible floaters
  840.     
  841.     HiliteShowHideFloatingWindows(resuming,true);
  842.     }
  843.  
  844. void
  845. HiliteWindowsForModalDialog(Boolean hiliting)
  846.     {
  847.     //    When we display a modal dialog, we need to unhighlight
  848.     //    all visible floaters. We also need to re-hilite them
  849.     //    afterwards.
  850.     
  851.     HiliteShowHideFloatingWindows(hiliting,false);
  852.     }
  853.  
  854. void
  855. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  856.     {
  857.     WindowPeek    aWindow;
  858.     TWindow *    wobj;
  859.     
  860.     HiliteAndActivateWindow(FrontNonFloatingWindow(),hiliting);
  861.  
  862.     aWindow = (WindowPeek) LMGetWindowList();
  863.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  864.         {
  865.         wobj = GetWindowObject((WindowPtr) aWindow);
  866.         
  867.         //    If we are hiding or showing, only hide/show windows
  868.         //    that were visible to begin with.
  869.         
  870.         //    NOTE:    We use our copy of the visible flag so we can
  871.         //            automatically show floaters on a resume event.
  872.         
  873.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  874.         //            really need the “::” on ShowHide, but as long
  875.         //            as we’re trying to avoid ambiguity.
  876.         
  877.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  878.             ::ShowHide((WindowPtr) aWindow,hiliting);
  879.             
  880.         //    All floaters are hilited if any floater is hilited
  881.  
  882.         HiliteWindow((WindowPtr) aWindow,hiliting);
  883.         aWindow = (WindowPeek) aWindow->nextWindow;
  884.         }
  885.     }
  886.  
  887.  
  888. ///////////////////////////////////////////////////////////////////////////
  889. //
  890. //    Routines used for dealing with windows and multiple screens
  891. //
  892.  
  893. pascal void
  894. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  895.     {
  896.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  897.     long                                windowAreaOnThisScreen;
  898.     Rect                                windowRectOnThisScreen;
  899.     
  900.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  901.  
  902.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  903.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  904.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  905.  
  906.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  907.         {
  908.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  909.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  910.         }
  911.     }
  912.  
  913.  
  914. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  915.  
  916.  
  917. void
  918. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  919.     {
  920.     RgnHandle                            copyOfWindowStrucRgn;
  921.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  922.  
  923.     //    Use DeviceLoop to find out what GDevice contains the largest
  924.     //    portion of the supplied window.
  925.     //
  926.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  927.     //            the window strucRgn, not contRgn.
  928.  
  929.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  930.     deviceLoopData.fLargestArea = -1;
  931.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  932.     
  933.     copyOfWindowStrucRgn = NewRgn();
  934.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  935.  
  936.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  937.  
  938.     DisposeRgn(copyOfWindowStrucRgn);
  939.     
  940.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  941.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  942.  
  943.     //    Leave some space around the edges of the screen so window look good, AND
  944.     //    if the best device is the main screen, leave space for the Menubar
  945.     
  946.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  947.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  948.         theBestScreenRect->top += GetMBarHeight();
  949.     }
  950.  
  951.  
  952. ///////////////////////////////////////////////////////////////////////////
  953. //
  954. //    Drag Manager callback routines which dispatch to a window’s method
  955. //
  956.  
  957. pascal OSErr
  958. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  959.     {
  960.     TWindow *wobj = GetWindowObject(theWindow);
  961.     
  962.     if (wobj)
  963.         return(wobj->HandleDrag(dragMessage,theDrag));
  964.     else
  965.         return dragNotAcceptedErr;
  966.     }
  967.  
  968.     
  969. pascal OSErr
  970. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  971.     {
  972.     TWindow *wobj = GetWindowObject(theWindow);
  973.     
  974.     if (wobj)
  975.         return(wobj->HandleDrop(theDrag));
  976.     else
  977.         return dragNotAcceptedErr;
  978.     }
  979.